package com.dingreading.cloud.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.app.entity.Corporations;
import com.dingreading.cloud.app.entity.Staff;
import com.dingreading.cloud.app.entity.StaffRoles;
import com.dingreading.cloud.app.entity.Stores;
import com.dingreading.cloud.app.service.CorporationsService;
import com.dingreading.cloud.app.service.StaffRolesService;
import com.dingreading.cloud.app.service.StaffService;
import com.dingreading.cloud.app.service.StoresService;
import com.dingreading.cloud.app.util.UidUtil;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.dto.AppStaffDto;
import com.dingreading.cloud.common.util.MD5Util;
import com.dingreading.cloud.common.util.StaffPwdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/staff")
@Api(tags = "APP-店员管理")
public class AppStaffController {

    @Resource
    private CorporationsService corporationsService;
    @Resource
    private StoresService storesService;
    @Resource
    private StaffService staffService;
    @Resource
    private StaffRolesService staffRolesService;

    @ApiOperation(value = "店员登录")
    @PostMapping(value = "/login", produces = "application/json; charset=UTF-8")
    public R<Object> login(
            @ApiParam(value = "工作手机号") @RequestParam(value = "phone") String phone,
            @ApiParam(value = "密码") @RequestParam(value = "password") String password
    ) {
//        if (!RegularUtil.isValidPhoneNumber(phone)) return R.fail("手机号错误");
//        if (!password.equals("123456")) return R.fail("密码错误");
//
//        AppStaffDto staff = staffService.getByPubPhoneV2(phone);
//        if (staff == null) return R.fail("员工不存在");
//        if (!staff.getEnabled().equals(1)) return R.fail("员工已被禁用");
//
//        // 根据角色权限获取门店
//        String uid = staff.getUid();
//        List<StaffRoles> staffRolesList = staffRolesService.getByStaffUidAndEnable(uid, 1);
//        if (CollectionUtils.isNotEmpty(staffRolesList)) {
//            StaffRoles staffRoles = staffRolesList.get(0);
//            Stores stores = storesService.getByUid(staffRoles.getStoreUid());
//            if (stores != null) {
//                staff.setStoreUid(stores.getUid());
//                staff.setStoreName(stores.getStoreName());
//            }
//        }
//
//        return R.ok(staff);

        return R.fail("功能已暂停，请升级App版本");
    }

    @ApiOperation(value = "扫码app店员登录V2")
    @PostMapping(value = "/v2/login", produces = "application/json; charset=UTF-8")
    public R<Object> loginV2(
            @ApiParam(value = "账号") @RequestParam(value = "username") String username,
            @ApiParam(value = "密码") @RequestParam(value = "password") String password
    ) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password))
            return R.fail("账号或密码错误");

        Staff staff = staffService.getByUsername(username);
        if (null == staff) return R.fail("员工账号获取失败");
        if (!staff.getEnabled().equals(1)) return R.fail("员工已被禁用");

        String pwdSource = StaffPwdUtil.getPwdSource(password, staff.getPwdSalt());
        String encodedPwd = MD5Util.MD5Encode(pwdSource);
        if (!encodedPwd.equals(staff.getEncodedPwd()))
            return R.fail("密码错误");

        Corporations corporations = corporationsService.getByUid(staff.getCorpUid());
        String judgeCorp = UidUtil.judgeCorp(corporations);
        if (StringUtils.isNotBlank(judgeCorp))
            return R.fail(judgeCorp);

        // 根据角色权限获取门店
        String uid = staff.getUid();
        List<StaffRoles> staffRolesList = staffRolesService.getByStoreManager(uid);
        if (CollectionUtils.isEmpty(staffRolesList))
            return R.fail("请用店长账号登录");
        StaffRoles staffRoles = staffRolesList.get(0);
        Stores stores = storesService.getByUid(staffRoles.getStoreUid());
        if (null == stores)
            return R.fail("门店信息获取失败");
        if (!stores.getEnabled().equals(1))
            return R.fail("门店已被禁用");

        AppStaffDto data = new AppStaffDto();
        data.setUid(staff.getUid());
        data.setNickName(staff.getNickName());
        data.setFullName(staff.getFullName());
        data.setPubPhone(staff.getPubPhone());
        data.setPriPhone(staff.getPriPhone());
        data.setDepartment(staff.getDepartment());
        data.setRole(staff.getRole());
        data.setEnabled(staff.getEnabled());
        data.setStoreUid(stores.getUid());
        data.setStoreName(stores.getStoreName());

        return R.ok(data);
    }

    @ApiOperation(value = "获取员工的所在门店")
    @PostMapping(value = "/getStaffStores", produces = "application/json; charset=UTF-8")
    public R<Object> getStaffStores(
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid
    ) {
        Object staffObj = UidUtil.getStaff(staffUid);
        if (staffObj instanceof String) return R.fail(staffObj.toString());
        Staff staff = (Staff) staffObj;

        Corporations corporations = corporationsService.getByUid(staff.getCorpUid());
        String judgeCorp = UidUtil.judgeCorp(corporations);
        if (StringUtils.isNotBlank(judgeCorp))
            return R.fail(judgeCorp);

        List<JSONObject> storeList = staffRolesService.getByStoreManagerList(staffUid);
        if (CollectionUtils.isNotEmpty(storeList) && storeList.size() > 1)
            return R.ok(storeList);
        return R.ok();
    }
}